package hu.diploma.android;

import hu.diploma.android.util.Context;
import hu.diploma.common.resource.GroupResource;
import hu.diploma.common.vos.GroupVo;

import java.util.List;

import org.restlet.resource.ClientResource;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class PeopleThread extends Thread {

	final static int DONE = 1;
	final static int RUNNING = 0;

	Handler mHandler;
	int mState;
	int total;

	PeopleThread(Handler h) {
		this.mHandler = h;
	}

	@Override
	public void run() {
		Log.d("DEBUG", "START PeopleThread");
		this.mState = PeopleThread.RUNNING;
		this.total = 1;
		while (this.mState == PeopleThread.RUNNING) {
			Log.d("DEBUG", "START PeopleThread1");
			ClientResource cr = new ClientResource("http://192.168.1.64:8080/web/restlet/group");
			Log.d("DEBUG", "START PeopleThread2");
			GroupResource resource = cr.wrap(GroupResource.class);
			Log.d("DEBUG", "START PeopleThread3");
			Bundle b = new Bundle();
			Log.d("DEBUG", "START PeopleThread4");
			List<GroupVo> groups = Context.getContext().getGroupVos();
			Log.d("DEBUG", groups.toString());
			int total = 1;
			for (GroupVo groupVo : groups) {
				b.putString("gruop", groupVo.getName());

				resource.merge(groupVo);
				int i = 100 / groups.size() * total;

				b.putInt("count", i);
				Message msg = this.mHandler.obtainMessage();
				msg.setData(b);
				this.mHandler.sendMessage(msg);
				total++;

			}
			b.putInt("state", PeopleThread.DONE);
			Message msg = this.mHandler.obtainMessage();
			msg.setData(b);
			this.mHandler.sendMessage(msg);
			this.mState = PeopleThread.DONE;
		}
	}

	public void setState(int state) {
		this.mState = state;
	}
}